﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyMat
{
    //矩阵，包括存储矩阵和基本的运算
    public class MyMatrix
    {
        private MyVector[] matrix;
        private int row;//行
        bool isEchelon=false;
        private int column;//列
        //赋值的行不整齐将无法赋值
        public MyVector[] Date
        {
            get { return matrix; }
            set {
                isEchelon = false;
                column = value.Length;
                if (column > 0)
                    row = value[0].Date.Length;
                else
                {
                    row = 0;
                    matrix = null;
                } 
                for (int i = 1; i < column; ++i)
                    if (value[i].Date.Length != row)
                    {
                        row = 0;
                        column = 0;
                        matrix = null;
                    }
                matrix = value;
            }
        }
        //化为阶梯型矩阵
        public void Change_Echelon()
        {
            if (matrix == null)
                return;
            int x=0;//当前列与当前行
            for (int y = 0; (y < column - 1)&&(x<row-1); ++y,++x)
            {
                //交换一个有有效值的到当前行
                if (matrix[y].Date[x]==0)
                {
                    bool havevalue = false;//本列有有效的值
                    for (int j = y + 1; j < column; ++j)
                    {
                        double searchPoint = matrix[j].Date[x];
                        if (searchPoint ==0)
                            continue;
                        MyVector temp = matrix[y];
                        matrix[y] = matrix[j];
                        matrix[j] = temp;
                        havevalue = true;
                        break;
                    }
                    if (!havevalue)//无有效值，下一列
                    {
                        --y;
                        continue;
                    }
                }
                //消去
                for (int j = y + 1; j < column; ++j)
                {
                    double goalkill=matrix[j].Date[x];
                    if (goalkill==0)
                        continue;
                    goalkill /= matrix[y].Date[x];
                    MyVector tempVect= goalkill*matrix[y];
                    matrix[j] -= tempVect;
                    matrix[j].Date[x] = 0;
                }
             }
            isEchelon = true;
        }
        //化为简化阶梯型
        public void Change_Simple()
        {
            Change_Echelon();
            for (int i = column - 1; i >= 0; --i)
            {
                int j;//j为消元
                for (j=0; j < row; ++j)
                    if (matrix[i].Date[j]!=0)
                        break;
                if (j < row)
                    matrix[i] /= matrix[i].Date[j];
                else
                    continue;
                for (int m = i - 1; m >= 0; --m)
                {
                    double goalkill = matrix[m].Date[j];
                    if (goalkill ==0)
                        continue;
                    goalkill /= matrix[i].Date[j];
                    matrix[m] -= goalkill * matrix[i];
                }
            }
            
        }
       //计算方阵的行列式（目前仅支持阶梯型）
        public double? getDeterminantValue()
        {
            if (isEchelon&&(row==column))
            {
                double result=matrix[0].Date[0];
                for (int i = 1; i < matrix.Length; ++i)
                    result*=matrix[i].Date[i];
                return result;
            }
            return null;
        }
    }
}
